package com.salon.client.widgets.suggest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.extjs.gxt.ui.client.Registry;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.SuggestOracle;
import com.salon.client.GreetingServiceAsync;
import com.salon.client.Salon;
import com.salon.client.beans.MaterialBean;
import com.salon.client.beans.ParentPagingLoadResult;
import com.salon.client.beans.WhereBean;
import com.salon.client.beans.WhereBean.Operator;

public class MaterialSuggest extends SuggestOracle {
	public enum FIELD {NAME};
	public static final int MAXSUGGESTIONS = 50;
	
	private com.google.gwt.user.client.ui.SuggestOracle.Request currentRequest;
	private com.google.gwt.user.client.ui.SuggestOracle.Callback currentCallback;
	private com.google.gwt.user.client.ui.SuggestOracle.Request nextRequest;
	private com.google.gwt.user.client.ui.SuggestOracle.Callback nextCallback;
	private static List<String> recentSuggestions = new ArrayList<String>();
	
	private FIELD field = FIELD.NAME;
	
	public MaterialSuggest(FIELD field) 
	{
		this.field = field;	
	}
	
	@Override
	public boolean isDisplayStringHTML() {
		return false;
	}
	
	public void requestSuggestions(Request request, Callback callback) {
		if (currentRequest == null) {
			currentRequest = request;
			currentCallback = callback;
			sendRequest(request, callback);
		} else {
			nextRequest = request;
			nextCallback = callback;
		}
	}

	private void sendRequest(Request request, Callback callback) {
		WhereBean[] w = request.getQuery() == null || request.getQuery().trim().length() == 0
			? null : new WhereBean[] {new WhereBean("CONCAT_WS('_', name, code)", 
				("%" + request.getQuery() + "%").toUpperCase(), Operator.LIKE)};
		List<WhereBean> wheres = Arrays.asList(w);
		((GreetingServiceAsync) Registry.get(Salon.SERVICE))
		.getMaterials(
				0, request.getLimit(), wheres,
				new AsyncCallback<ParentPagingLoadResult<MaterialBean>>() {

					public void onFailure(Throwable caught) {
						if (nextRequest != null) {
							currentRequest = nextRequest;
							currentCallback = nextCallback;
							nextRequest = null;
							nextCallback = null;
							sendRequest(currentRequest, currentCallback);
						} else {
							Response resp = new Response();
							resp.setSuggestions(new ArrayList<ParentSuggestion>());
							currentCallback.onSuggestionsReady(currentRequest, resp);
							currentRequest = null;
							currentCallback = null;
						}

					}

					public void onSuccess(ParentPagingLoadResult<MaterialBean> result) {
						if (nextRequest != null) {
							currentRequest = nextRequest;
							currentCallback = nextCallback;
							nextRequest = null;
							nextCallback = null;
							sendRequest(currentRequest, currentCallback);
						} else {
							Response resp = new Response();
							List<Suggestion> sgs = new ArrayList<Suggestion>();
							List<MaterialBean> pats = result.getData();
							for(MaterialBean pat : pats) 
							{
								ParentSuggestion s = new ParentSuggestion();
								s.setDisplayString(pat.getDisplayF());
								s.setObjKey(pat.getId());
								s.setBean(pat);
								sgs.add(s);
							}
							resp.setSuggestions(sgs);
							currentCallback.onSuggestionsReady(currentRequest, resp);
							currentRequest = null;
							currentCallback = null;
						}
					}});
	}

	public static void clearRecents() {
		recentSuggestions.clear();
	}

	public void addRecentSelection(String patientKey) {
		if ((patientKey == null) || !patientKey.startsWith("patient:"))
			return;
		
		recentSuggestions.remove(patientKey);
		recentSuggestions.add(0, patientKey);
	}
	
	public void requestDefaultSuggestions(Request request, Callback callback) {
		List<ParentSuggestion> defaults = new ArrayList<ParentSuggestion>();
		
		Response resp = new Response();
		resp.setSuggestions(defaults);
		callback.onSuggestionsReady(request, resp);
	}
}